package com.tools
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class UIHelper
	{
		static public var self:UIHelper=new UIHelper();
		public function UIHelper()
		{
		}
		/**设置对象属性 例：setAttr(sp,{name:"sp",x:100,y:100,width:50,height:50...})**/
		public function setAttr(obj:DisplayObject,param:Object):void{
			if(!obj||!param)return;
			if(param.parent)param.parent.addChild(obj);
			if(param.name)obj.name=param.name;
			if(param.x)obj.x=param.x;
			if(param.y)obj.y=param.y;
			if(param.width)obj.width=param.width;
			if(param.height)obj.height=param.height;
			if(obj is TextField&&param.text)obj["text"]=param.text;
			if(param.visible!=null)obj.visible=param.visible;
			if(obj is TextField&&param.htmlText)obj["htmlText"]=param.htmlText;
			if(param.mouseEmabled!=null)obj["mouseEnabled"]=param.mouseEnabled;
			if((obj is Sprite||obj is MovieClip)&&param.mouseChildren!=null)obj["mouseChildren"]=param.mouseChildren;
		}
		/**创建并返回一个textField对象，并设置属性**/
		public function createTxt(param:Object=null):TextField{
			var txt:TextField=new TextField();
			var tf:TextFormat=new TextFormat();
			if(param.font)tf.font=param.font;
			if(param.color)tf.color=param.color;
			if(param.size)tf.size=param.size;
			if(param.align)tf.align=param.align;
			if(param.underline!=null)tf.underline=param.underline;
			if(param.bold!=null)tf.bold=param.bold;
			if(param.url)tf.url=param.url;
			txt.defaultTextFormat=tf;
			if(param.name)txt.name=param.name;
			if(param.x)txt.x=param.x;
			if(param.y)txt.y=param.y;
			if(param.width)txt.width=param.width;
			if(param.height)txt.height=param.height;
			if(param.visible!=null)txt.visible=param.visible;
			if(param.htmlText)txt.htmlText=param.htmlText;
			if(param.mouseEnabled!=null)txt.mouseEnabled=param.mouseEnabled;
			if(param.multiline!=null)txt.multiline=param.multilinet;
			if(param.wordWrap!=null)txt.wordWrap=param.worldWrap;
			if(param.parent)param.parent.addChild(txt);
			if(param.borderColor)txt.borderColor=param.borderColor;
			if(param.border!=null)txt.border=param.border;
			if(param.type)txt.type=param.type;
			if(param.text)txt.text=param.text;
			if(param.parent)param.parent.addChild(txt);
			return txt;
		}
		/**
		 *创建并返回一个sprite对象 
		 * @param param 参数列表{name,x,y,width,height,childList,visible,parent,mouseEnabled,mouseChildren}
		 * @param param_childList 要添加到该sprite的显示对象列表，数组格式
		 * @param param_parent 父容器，即将该sprite添加到哪个容器中，有则添加
		 * @return 
		 * 
		 */		
		public function createSprite(param:Object=null):Sprite{
			var sp:Sprite=new Sprite();
			if(param.name)sp.name=param.name;
			if(param.x)sp.x=param.x;
			if(param.y)sp.y=param.y;
			if(param.parent)param.parent.addChild(sp);
			if(param.visible!=null)sp.visible=param.visible;
			if(param.mouseEnabled!=null)sp.mouseEnabled=param.mouseEnabled;
			if(param.mouseChildren!=null)sp.mouseChildren=param.mouseChildren;
			if(param.buttonMode!=null)sp.buttonMode=param.buttonMode;
			if(param.childList&&param.childList is Array){
				while(param.childList.length>0){
					if(param.childList[0] is DisplayObject)sp.addChild(param.childList[0]);
					param.childList.shift();
				}
			}			
			if(param.width)sp.width=param.width;
			if(param.height)sp.height=param.height;
			return sp;
		}
		/**设置textField的样式**/
		public function setFormat(txt:TextField,param:Object=null):void{
			var tf:TextFormat=new TextFormat();
			if(param.font)tf.font=param.font;
			if(param.color)tf.color=param.color;
			if(param.size)tf.size=param.size;
			if(param.align)tf.align=param.align;
			if(param.underline!=null)tf.underline=param.underline;
			if(param.bold!=null)tf.bold=param.bold;
			if(param.url)tf.url=param.url;
			txt.defaultTextFormat=tf;
		}
		/**文本描边**/
		public function txtFilter(txt:TextField,color:uint=0x000000,alpha:Number=1,blurX:Number=6.0,blurY:Number=6.0,strength:Number=2,quality:int=1,inner:Boolean=false,knockout:Boolean=false):void{
			var tg:GlowFilter=new GlowFilter(color,alpha,blurX,blurY,strength,quality,inner,knockout);
			txt.filters=[tg];
		}
		/**兼容用的，新版本可删除**/
		public function createBtn(name:String,parent:Sprite,child:Array,x:int=0,y:int=0,w:int=-1,h:int=-1,vis:Boolean=true):Sprite{
			var btn:Sprite=new Sprite();
			btn.name=name;
			var i:int;
			for(i=0;i<child.length;i++){
				btn.addChild(child[i]);
			}
			btn.x=x;
			btn.y=y;
			btn.visible=vis;
			btn.buttonMode=true;
			btn.mouseChildren=false;
			parent.addChild(btn);
			if(w!=-1)btn.width=w;
			if(h!=-1)btn.height=h;
			return  btn;
		}
	}
}